package me.luger.core.commom.util;

import okhttp3.*;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by a on 2017/8/14.
 */
public class OkHttpUtil {
    public static String doGet(String url, Map<String, Object> params) {
        OkHttpClient okHttpClient = new OkHttpClient();
        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        if(null != params){
            for(Map.Entry<String,Object> entry : params.entrySet()){
                urlBuilder.addQueryParameter(entry.getKey(),StringUtil.value2String(entry.getValue()));
            }
        }
        String fullUrl = urlBuilder.build().toString();
        Request request = new Request.Builder()
                .url(fullUrl)
                .build();

        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void main(String args[]){
        DateUtil.timeMarker(false);
        Map<String,Object> params = new HashMap<String,Object>();
        List<String> lines = null;
        try {
            lines = FileUtil.readLines(new File("C:\\Users\\a\\Desktop\\pidsyumin20170808.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        params.put("pn",0);
        params.put("rn",100);
        params.put("allFree","1");
        String url = "http://nplserver.kuwo.cn/pl.svc?op=getlistinfo&encode=utf-8&keyset=pl2012&identity=kuwo&vipver=1";
        for(String line : lines){
            params.put("pid",line);
            System.out.println(doGet(url,params));
        }
        DateUtil.timeMarker(true);
    }

}
